<template>
  <div class="app-container">

     <!-- 批量添加货物信息对话框 -->
    <el-dialog :title="title" :visible.sync="open" width="500px" append-to-body>
      <el-form ref="form" :model="form" :rules="rules" label-width="120px">
        <!--el-form-item label="捆包号" prop="bundleNumber">
          <el-input v-model="form.bundleNumber" placeholder="请输入捆包号" />
        </el-form-item-->
        <el-form-item label="货物类型" prop="goodsType">
          <el-select v-model="form.goodsType" placeholder="请选择货物类型" style="width: 340px;">
            <el-option
              v-for="dict in dict.type.goods_type"
              :key="dict.value"
              :label="dict.label"
              :value="dict.label"
            ></el-option>
          </el-select>
        </el-form-item>
        <!--el-form-item label="产地" prop="producer">
          <el-input v-model="form.producer" placeholder="请输入产地" />
        </el-form-item-->
        <el-form-item label="品名" prop="product">
          <el-input v-model="form.product" placeholder="请输入品名" />
        </el-form-item>
        <el-form-item label="规格" prop="specification">
          <el-input v-model="form.specification" placeholder="请输入规格" />
        </el-form-item>
        <el-form-item label="材质" prop="steelGrade">
          <el-input v-model="form.steelGrade" placeholder="请输入材质" />
        </el-form-item>
        <el-form-item label="总重量(吨)" prop="weight">
          <el-input v-model="form.weight" placeholder="请输入重量" />
        </el-form-item>
        <el-form-item label="总件数" prop="numberOfPiece">
          <el-input v-model="form.numberOfPiece" placeholder="请输入件数" />
        </el-form-item>
        <!--el-form-item label="合约号" prop="agreementNumber">
          <el-input v-model="form.agreementNumber" placeholder="请输入合约号" />
        </el-form-item>
        <el-form-item label="合同号" prop="contractNumber">
          <el-input v-model="form.contractNumber" placeholder="请输入合同号" />
        </el-form-item-->

        <el-form-item label="园区信息" prop="logisticsParkName">
          <el-select ref="optionRef" v-model="form.logisticsParkName" placeholder="请选择园区信息" style="width: 340px;" @change="handleChangeLogisticsPark">
            <el-option
              v-for="dict in parkLocateOptions"
                :key="dict.locateId"
                :label="dict.name"
                :value="dict.name"
              ></el-option>
          </el-select>
        </el-form-item>
        <el-form-item label="仓位信息" prop="warehouseLocateName">
          <el-select v-model="form.warehouseLocateName" placeholder="请选择仓位信息" style="width: 340px;" @change="handleChangeWarehouseLocate">
            <el-option
              v-for="dict in warehouseLocateOptions"
                :key="dict.locateId"
                :label="dict.name"
                :value="dict.name"
              ></el-option>
          </el-select>
        </el-form-item>
        <el-form-item label="区位信息" prop="regionLocateName">
          <el-select v-model="form.regionLocateName" placeholder="请选择区位信息" style="width: 340px;" @change="handleChangeRegionLocate">
            <el-option
              v-for="dict in regionLocateOptions"
                :key="dict.locateId"
                :label="dict.name"
                :value="dict.name"
              ></el-option>
          </el-select>
        </el-form-item>
        <el-form-item label="货位信息" prop="goodsLocateName">
          <el-select v-model="form.goodsLocateName" placeholder="请选择货位信息" style="width: 340px;" @change="handleChangeGoodsLocate">
            <el-option
              v-for="dict in goodsLocateOptions"
                :key="dict.locateId"
                :label="dict.name"
                :value="dict.name"
              ></el-option>
          </el-select>
        </el-form-item>

        <el-form-item label="货权客户" prop="goodsRightCustomer">
          <el-select v-model="form.goodsRightCustomer" placeholder="请选择货权客户" style="width: 340px;" filterable>
            <el-option
              v-for="dict in customerOptions"
                :key="dict.customerId"
                :label="dict.customerName"
                :value="dict.customerName"
              ></el-option>
          </el-select>
        </el-form-item>

      </el-form>
      <div slot="footer" class="dialog-footer">
        <el-button type="primary" @click="submitForm">确 定</el-button>
        <el-button @click="cancel">取 消</el-button>
      </div>
    </el-dialog>
  </div>
</template>

<script>
import { addGood, updateGood, batchAddGood } from "@/api/goods/good";
import { listCustomerOption } from "@/api/customs/custom";
//import { listFlowOption, updateFlow } from "@/api/system/flow";
import { listLocateOption } from "@/api/locates/locate";
import { isNotNull } from "@/utils/validate";

export default {
  name: "BatchGoods",
  dicts: ['warehouse_type', 'sys_yes_no', 'settlement_mode', 'price_mode', 'distribute_goods_status', 'goods_type', 'inventory_status', 'check_status'],
  data() {
    return {
      // 定义列表高度
      tableHeight: 180,
      // 定义查询高度
      queryHeight: 200,
      // 遮罩层
      loading: true,
      // 选中数组
      ids: [],
      // 非单个禁用
      single: true,
      // 非多个禁用
      multiple: true,
      // 大于1个禁用
      zeroOne: false,
      // 显示搜索条件
      showSearch: true,
      // 总条数
      total: 0,
      // 货物信息表格数据
      goodList: [],
      // 弹出层标题
      title: "",
      // 是否显示弹出层
      open: false,
      // 三方编号时间范围
      daterangeProduceDate: [],
      // 三方编号时间范围
      daterangeEnterWarehouseTime: [],
      // 三方编号时间范围
      daterangeEnterWarehouseConfirmTime: [],
      // 三方编号时间范围
      daterangeExitWarehouseTime: [],
      // 三方编号时间范围
      daterangeExitWarehouseConfirmTime: [],
      // 三方编号时间范围
      daterangeAdjustGoodsTime: [],
      // 三方编号时间范围
      daterangeCheckGoodsTime: [],
      // 三方编号时间范围
      daterangeGoodsTransferTime: [],
      // 三方编号时间范围
      daterangeGoodsTransferAuditTime: [],
      // 三方编号时间范围
      daterangeCreateTime: [],
      // 查询参数
/*      queryParams: {
        pageNum: 1,
        pageSize: 10,
        bundleNumber: null,
        goodsType: null,
        producer: null,
        product: null,
        specification: null,
        steelGrade: null,
        weight: null,
        numberOfPiece: null,
        agreementNumber: null,
        contractNumber: null,
        produceDate: null,
        recordSource: null,
        inventoryStatus: null,
        warehouseType: null,
        logisticsParkName: null,
        warehouseLocateName: null,
        regionLocateName: null,
        goodsLocateName: null,
        layerLocateName: null,
        warehouseTotalLocate: null,
        enterWarehouseNumber: null,
        enterWarehouseCheck: null,
        enterWarehouseCustomer: null,
        enterWarehouseDriver: null,
        enterWarehouseVehicle: null,
        enterWarehousePerson: null,
        enterWarehouseTime: null,
        enterWarehouseConfirmPerson: null,
        enterWarehouseConfirmTime: null,
        exitWarehouseNumber: null,
        exitWarehouseCheck: null,
        exitWarehouseCustomer: null,
        exitWarehouseDriver: null,
        exitWarehouseVehicle: null,
        exitWarehousePerson: null,
        exitWarehouseTime: null,
        exitWarehouseConfirmPerson: null,
        exitWarehouseConfirmTime: null,
        adjustGoodsPerson: null,
        adjustGoodsTime: null,
        checkGoodsPerson: null,
        checkGoodsTime: null,
        originGoodsRightCustomer: null,
        goodsRightCustomer: null,
        goodsTransferTimes: null,
        goodsTransferNumber: null,
        goodsTransferPerson: null,
        goodsTransferTime: null,
        goodsTransferAuditPerson: null,
        goodsTransferAuditTime: null,
        goodsTransferIds: null,
        indoorWarehousePrice: null,
        outdoorWarehousePrice: null,
        storageFee: null,
        freeStorage: null,
        overduePriceMode: null,
        overduePrice: null,
        storageDay: null,
        overdueFee: null,
        stackTransferPrice: null,
        stackTransferFee: null,
        warehouseSettlementBillNumber: null,
        warehouseSettlementCustomer: null,
        goodsDamage: null,
        goodsDamagePicture: null,
        goodsRemark: null,
        distributeGoodsStatus: null,
        distributeGoodsCart: null,
        sendGoodsCustomer: null,
        endFlowDirection: null,
        endUnloadPoint: null,
        wayBillNumber: null,
        paymentMode: null,
        freightPriceMode: null,
        freightPrice: null,
        freightAmount: null,
        primage: null,
        freightTotalAmount: null,
        settlementMethod: null,
        settlementPriceMode: null,
        settlementPrice: null,
        settlementAmount: null,
        premiumFreight: null,
        settlementTotalAmount: null,
        freightPaymentBillNumber: null,
        freightSettlementBillNumber: null,
        otherNumber: null,
        threePartyNumber: null,
        createBy: null,
        createTime: null,
      },*/
      // 表单参数
      form: {},
      // 字典项客户
      customerOptions: [],
      // 字典项始发流向
      beginFlowOptions: [],
      // 字典项目的流向
      endFlowOptions: [],
      // 字典项园区信息
      parkLocateOptions: [],
      // 字典项仓位信息
      warehouseLocateOptions: [],
      // 字典项区位信息
      regionLocateOptions: [],
      // 字典项货位信息
      goodsLocateOptions: [],
      //
      enterWarehouseNumber: null,
      //计划行
      enterWarehouseRow: [],
      //
      select: true,
      // 表单校验
      rules: {
        goodsType: [
          { required: true, message: "货物类型不能为空", trigger: "blur" }
        ],
        weight: [
          { required: true, message: "总重量不能为空", trigger: "blur" }
        ],
        numberOfPiece: [
          { required: true, message: "总件数不能为空", trigger: "blur" }
        ],
        logisticsParkName: [
          { required: false, message: "园区不能为空", trigger: "blur" }
        ],
        warehouseLocateName: [
          { required: false, message: "库位不能为空", trigger: "blur" }
        ],
        regionLocateName: [
          { required: false, message: "区位不能为空", trigger: "blur" }
        ],
      }
    };
  },
  created() {
    this.getOption();
  },
  methods: {
    init(open, title, enterWarehouseNumber, enterWarehouseRow) {
      this.reset();
      this.open = open;
      this.title = title;
      this.enterWarehouseNumber = enterWarehouseNumber;
      this.enterWarehouseRow = enterWarehouseRow;
      this.$set(this.form, 'goodsType', '带钢');
      this.$set(this.form, 'recordSource', 'PC');
      this.$set(this.form, 'inventoryStatus', '入库中');
      this.$set(this.form, 'enterWarehouseNumber', this.enterWarehouseNumber);
      this.$set(this.form, 'enterWarehouseCustomer', this.enterWarehouseRow.enterWarehouseCustomer);
      this.$set(this.form, 'enterWarehouseDriver', this.enterWarehouseRow.enterWarehouseDriver);
      this.$set(this.form, 'enterWarehouseVehicle', this.enterWarehouseRow.enterWarehouseVehicle);

      this.$set(this.form, 'goodsRightCustomer', this.enterWarehouseRow.enterWarehouseCustomer);
      
      this.$set(this.form, 'enterWarehousePerson', this.enterWarehouseRow.createBy);
      this.$set(this.form, 'enterWarehouseTime', this.enterWarehouseRow.enterWarehouseTime);
    },
    // 取消按钮
    cancel() {
      this.open = false;
      this.reset();
    },
    // 表单重置
    reset() {
      this.form = {
        goodsId: null,
        bundleNumber: null,
        goodsType: null,
        producer: null,
        product: null,
        specification: null,
        steelGrade: null,
        weight: null,
        numberOfPiece: null,
        agreementNumber: null,
        contractNumber: null,
        produceDate: null,
        recordSource: null,
        inventoryStatus: null,
        warehouseType: null,
        logisticsParkName: null,
        warehouseLocateName: null,
        regionLocateName: null,
        goodsLocateName: null,
        layerLocateName: null,
        warehouseTotalLocate: null,
        enterWarehouseNumber: null,
        enterWarehouseCheck: null,
        enterWarehouseCustomer: null,
        enterWarehouseDriver: null,
        enterWarehouseVehicle: null,
        enterWarehousePerson: null,
        enterWarehouseTime: null,
        enterWarehouseConfirmPerson: null,
        enterWarehouseConfirmTime: null,
        exitWarehouseNumber: null,
        exitWarehouseCheck: null,
        exitWarehouseCustomer: null,
        exitWarehouseDriver: null,
        exitWarehouseVehicle: null,
        exitWarehousePerson: null,
        exitWarehouseTime: null,
        exitWarehouseConfirmPerson: null,
        exitWarehouseConfirmTime: null,
        adjustGoodsPerson: null,
        adjustGoodsTime: null,
        checkGoodsPerson: null,
        checkGoodsTime: null,
        originGoodsRightCustomer: null,
        goodsRightCustomer: null,
        goodsTransferTimes: null,
        goodsTransferNumber: null,
        goodsTransferPerson: null,
        goodsTransferTime: null,
        goodsTransferAuditPerson: null,
        goodsTransferAuditTime: null,
        goodsTransferIds: null,
        indoorWarehousePrice: null,
        outdoorWarehousePrice: null,
        storageFee: null,
        freeStorage: null,
        overduePriceMode: null,
        overduePrice: null,
        storageDay: null,
        overdueFee: null,
        stackTransferPrice: null,
        stackTransferFee: null,
        warehouseSettlementBillNumber: null,
        warehouseSettlementCustomer: null,
        goodsDamage: null,
        goodsDamagePicture: null,
        goodsRemark: null,
        distributeGoodsStatus: null,
        distributeGoodsCart: null,
        sendGoodsCustomer: null,
        endFlowDirection: null,
        endUnloadPoint: null,
        wayBillNumber: null,
        paymentMode: null,
        freightPriceMode: null,
        freightPrice: null,
        freightAmount: null,
        primage: null,
        freightTotalAmount: null,
        settlementMethod: null,
        settlementPriceMode: null,
        settlementPrice: null,
        settlementAmount: null,
        premiumFreight: null,
        settlementTotalAmount: null,
        freightPaymentBillNumber: null,
        freightSettlementBillNumber: null,
        otherNumber: null,
        threePartyNumber: null,
        delFlag: null,
        createBy: null,
        createTime: null,
        updateBy: null,
        updateTime: null
      };
      this.resetForm("form");
    },
    /** 提交按钮 */
    submitForm() {
      this.$refs["form"].validate(valid => {
        if (valid) {
            this.form.enterWarehouseNumber = this.enterWarehouseNumber;
            this.form.recordSource = "PC";
            batchAddGood(this.form).then(response => {
              this.$modal.msgSuccess("新增成功");
              this.open = false;
//              this.getList();
              this.$parent.getList();
            });
          }
      });
    },
    getOption() {
      /** 字典项客户*/
      listCustomerOption().then(response => {
        this.customerOptions = response.data;

//        console.log('this.customerOptions',this.customerOptions);
      });
      /** 字典项流向 */
      var compare = function (obj1, obj2) {
          var val1 = obj1.endFlowClickNumber;
          var val2 = obj2.endFlowClickNumber;
          if (val1 < val2) {
              return 1;
          } else if (val1 > val2) {
              return -1;
          } else {
              return 0;
          }            
      } 
/*      listFlowOption().then(response => {
        this.beginFlowOptions = JSON.parse(JSON.stringify(response.data));
        this.endFlowOptions = JSON.parse(JSON.stringify(response.data));
        this.endFlowOptions.sort(compare);
//        console.log('this.beginFlowOptions', this.beginFlowOptions);
//        console.log('this.endFlowOptions',this.endFlowOptions);
      });
*/
      /** 字典项园区 */
      listLocateOption(0).then(response => {
        this.parkLocateOptions = response.data;

//        console.log('this.parkLocateOptions',this.parkLocateOptions);
      });
    },
    handleChangeLogisticsPark(value) {
//      console.log('handleChangeLogisticsPark', value);
//      console.log('this.$refs.optionRef.selected.value', this.$refs.optionRef.selected.value);
//      console.log('this.$refs.optionRef.selected.label', this.$refs.optionRef.selected.label);
//      console.log('this.$refs.optionRef.selected.key', this.$refs.optionRef.selected.key);
      let park = {};
      park = this.parkLocateOptions.find((item) => {
        return item.name === value; //筛选出匹配数据
      });
//      console.log('park', park);
//      this.form.logisticsParkName = value.name;
//      console.log('handleChangeLogisticsPark', label);
      /** 字典项仓库 */
      listLocateOption(park.locateId).then(response => {
        this.warehouseLocateOptions = response.data;
        this.regionLocateOptions = {};
        this.goodsLocateOptions = {};
        this.form.warehouseLocateName = '';
        this.form.regionLocateName = '';
        this.form.goodsLocateName = '';
//        console.log('this.warehouseLocateOptions',this.warehouseLocateOptions);
      });
    },
    handleChangeWarehouseLocate(value) {
//      console.log('handleChangeWarehouse', value);
      let warehouse = {};
      warehouse = this.warehouseLocateOptions.find((item) => {
        return item.name === value; //筛选出匹配数据
      });
      this.$set(this.form, 'warehouseType', warehouse.warehouseType);
//      console.log('warehouse', warehouse);
      /** 字典项区位 */
      listLocateOption(warehouse.locateId).then(response => {
        this.regionLocateOptions = response.data;
        this.goodsLocateOptions = {};
        this.form.regionLocateName = '';
        this.form.goodsLocateName = '';
//        console.log('this.regionLocateOptions',this.regionLocateOptions);
      });
    },
    handleChangeRegionLocate(value) {
//      console.log('handleChangeWarehouse', value);
      let region = {};
      region = this.regionLocateOptions.find((item) => {
        return item.name === value; //筛选出匹配数据
      });
//      console.log('region', region);
      /** 字典项货位 */
      listLocateOption(region.locateId).then(response => {
        this.goodsLocateOptions = response.data;
        this.form.goodsLocateName = '';

//        console.log('this.goodsLocateOptions',this.goodsLocateOptions);
      });
    },
    handleChangeGoodsLocate(value) {
      this.form.warehouseTotalLocate = this.form.logisticsParkName + this.form.warehouseLocateName + this.form.regionLocateName + this.form.goodsLocateName;
    },
    //
    handleLoadOptions(){
//      if(this.form.logisticsParkName != ''){
      if(isNotNull(this.form.logisticsParkName)){
//        console.log("this.form.logisticsParkName = ", this.form.logisticsParkName);
        let park = {};
        park = this.parkLocateOptions.find((item) => {
          return item.name === this.form.logisticsParkName; //筛选出匹配数据
        });
        /** 字典项仓库 */
        listLocateOption(park.locateId).then(response => {
          this.warehouseLocateOptions = response.data;

          if (this.form.warehouseLocateName != '') {
//            console.log("this.form.warehouseLocateName = ", this.form.warehouseLocateName);
            let warehouse = {};
            warehouse = this.warehouseLocateOptions.find((item) => {
              return item.name === this.form.warehouseLocateName; //筛选出匹配数据
            });
            /** 字典项区位 */
            listLocateOption(warehouse.locateId).then(response => {
              this.regionLocateOptions = response.data;
              if (this.form.regionLocateName != '') {
//                console.log("this.form.regionLocateName = ", this.form.regionLocateName);
                let region = {};
                region = this.regionLocateOptions.find((item) => {
                  return item.name === this.form.regionLocateName; //筛选出匹配数据
                });
                /** 字典项货位 */
                listLocateOption(region.locateId).then(response => {
                  this.goodsLocateOptions = response.data;
                });
              }
            });
          }
        });
      }

    },

  }
};
</script>
